import { AxiosRequestHeaders } from "axios";
import { defineStore } from "pinia";
import { reactive, ref } from "vue";
import { Names } from "./namespace";
import { Config, EventBus } from "../main";
import { Local } from "../entity/helper/storage.helper";
import { RouteRecordName } from "vue-router";

export const useCommonStore = defineStore(Names.Common, () => {
  const routerData = ref<any>(null);
  const routerStack = reactive(new Map());
  const productionEnv = ref<boolean>(false);

  /**
   * 请求头管理
   */
  const requestHeader = reactive<AxiosRequestHeaders>(Local.get("requestHeader") || {});
  const getRequestHeader = () => requestHeader;
  const setRequestHeader = (key: string, value: string) => {
    requestHeader[key] = value;
    Local.set("requestHeader", requestHeader);
  };

  /**
   * Layout隐藏
   */
  const hideLayout = ref<boolean>((Local.get(Config.Store.layOutShow) as boolean) || false);
  const getHideLayout = () => hideLayout.value;
  const setHideLayout = (value: boolean) => {
    hideLayout.value = value;
    Local.set(Config.Store.layOutShow, value);
    EventBus.emit(Config.Event.changeLayoutShow, value);
  };
  return {
    routerData,
    routerStack,
    productionEnv,
    getRequestHeader,
    setRequestHeader,
    getHideLayout,
    setHideLayout,
  };
});
